package com.jvup.common.widget.adapter.pager;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

/**
 * 通用简单的基于fragment为子的PagerAdapter，用于TabLayout
 */
public class SimpleFragmentPagerAdapter extends FragmentStatePagerAdapter {

    private static final int BEHAVIOR_SET_USER_VISIBLE_HINT = 0;

    private final FragmentWrapper[] fragmentWrappers;

    public SimpleFragmentPagerAdapter(@NonNull FragmentManager fm, FragmentWrapper[] fragmentWrappers) {
        super(fm, BEHAVIOR_SET_USER_VISIBLE_HINT);
        this.fragmentWrappers = fragmentWrappers;
    }

    @Override
    public int getCount() {
        return fragmentWrappers.length;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragmentWrappers[position].fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentWrappers[position].title;
    }

    /**
     * Fragment包装器,绑定一个fragment的title
     */
    public static class FragmentWrapper {
        public final String title;
        public final Fragment fragment;
        public FragmentWrapper(String title, Fragment fragment) {
            this.title = title;
            this.fragment = fragment;
        }
    }
}